import https from '../../utils/Newrequests'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        type: 2,
        endstartlist: [],
        list: [],
        markingPapers: false,
        carryOutFlag: false,
        examlist: [],
        page: 1,
        pageSize: 10,
        isLoading: false,
        isEnd: false,
        isError: false,
        dataType: 1,
        examEndlist: [],
        teacherGradeQuery: 1, //成绩单
        teacherAnalysisQuery: 1,  //成绩分析
    },
    // changeState(e) {
    //     let index = e.currentTarget.dataset.index
    //     this.setData({
    //         carryOutFlag: !this.data.carryOutFlag,
    //         dataType: index
    //     })
    //     this.isLoading = false
    //     if (index == 1) {
    //         // this.endmytasks()
    //         this.getexamContent()
    //     } else if (index == 2) {
    //         // this.endmytasks()
    //         this.endExamproject()
    //     }
    // },
    changeState(e) {
        let index = e.currentTarget.dataset.index;
        if (this.data.dataType === index) return; // 点击相同tab不切换
        this.setData({
            dataType: index
        });
        this.isLoading = false;

        if (index == 1) {
            this.getexamContent();   // 获取进行中列表
        } else if (index == 2) {
            this.endExamproject();   // 获取已结束列表
        }
    },

    //配置获取
    async getSchoolConfig(examId) {
        let options = {
            url: `/analysis/gradeQueryPermissionConfig/getSchoolConfig/${examId}`,
            method: 'get',
            header: { "content-type": "application/json;charset=UTF-8" },
        };
        let res = await https(options)
        if (res.data.code == 200) {
            // console.log(res.data.data, "配置信息");
            const cfg = res.data.data || {};
            this.setData({
                teacherGradeQuery: cfg.teacherGradeQuery || 0,   // 没有就给 0，默认禁用
                teacherAnalysisQuery: cfg.teacherAnalysisQuery || 0
            })
        } else {
            wx.showToast({
                title: res.data.msg,
                icon: 'success',
                duration: 2000
            })
            // 出错时也置灰
            this.setData({
                teacherGradeQuery: 0,
                teacherAnalysisQuery: 0
            })
        }
    },
    //获取项目列表
    async getexamContent() {
        let { page, pageSize } = this.data
        let options = {
            url: '/task/teacher/stats/myProject',
            method: 'get',
            data: { pageNum: page, pageSize: pageSize, stopped: 0 },
            header: { "content-type": "application/json;charset=UTF-8" }
        }
        let res = await https(options)
        if (res.data.code == 200) {

            this.setData({
                examlist: res.data.data || [],
            })
        } else {
            this.setData({ isError: true })
        }
    },
    jumppage1(e) {
        wx.setStorageSync('newdataObj', JSON.stringify(e.currentTarget.dataset.item))
        wx.navigateTo({
            url: '/pages/newmarkinglist/newmarkinglist',
        })
    },
    jumppage(e) {
        let id = e.currentTarget.dataset.id
        let item = e.currentTarget.dataset.item;
        if (id != null) {

        }
        this.setData({
            type: e.currentTarget.dataset.item.n4type
        })
        if (!this.data.markingPapers) {
            let obj = {
                n4: e.currentTarget.dataset.item.nav4Id,
                n5: e.currentTarget.dataset.item.oldExamId,
                type: e.currentTarget.dataset.item.n4type,
                title: e.currentTarget.dataset.item.title,
                createdata: e.currentTarget.dataset.item.createdate,
                groupid: e.currentTarget.dataset.item.groupId,
            }
            wx.setStorageSync('alaynisData', JSON.stringify(obj))
        }
        if (id == 1) {
            if (id == 1 && this.data.markingPapers) {
                // 跳转阅卷
                wx.navigateTo({
                    url: '/pages/newmarkinglist/newmarkinglist',
                })
            } else {
                wx.showToast({
                    title: '当前进度不可使用该功能!',
                    icon: "none"
                })
            }
        }
        if (id == 2) {
            if (id == 2 && e.currentTarget.dataset.item.selectScoreType != 0) {
                let selectTypeName = item.selectTypeName;
                let idS = item.id
                // 跳转成绩单
                wx.navigateTo({
                    url: `/pages/Newselecttranscript/Newselecttranscript?selectTypeName=${selectTypeName}&id=${idS}`,
                })
            } else {
                wx.showToast({
                    title: '该时间无法查看成绩单,请根据通知时间查看!',
                    icon: "none"
                })
            }
        }
        if (id == 3) {
            if (id == 3 && e.currentTarget.dataset.item.selectScoreType != 0) {
                let selectTypeName = item.selectTypeName;
                let idS = item.id
                // 跳转成绩分析
                wx.navigateTo({
                    url: `/pages/Newselecttranscripts/Newselecttranscripts?selectTypeName=${selectTypeName}&id=${idS}`,
                });
            } else {
                wx.showToast({
                    title: '该时间无法查看成绩分析,请根据通知时间查看!',
                    icon: "none"
                })
            }
        }
    },
    // 已结束的项目
    async endExamproject() {
        let options = {
            url: '/system/exam/main/list/by/teacher',
            method: 'get',
            data: {
                pageNum: 1,         // 固定的页码
                pageSize: 15,       // 固定的每页数量
                stopped: 0          // 固定的 stopped 参数
            },
            header: { "content-type": "application/json;charset=UTF-8" }
        };

        try {
            // 发送请求并获取响应
            let res = await https(options);
            // 设置获取到的数据
            this.setData({ examEndlist: res.data.rows });
            if (this.data.examEndlist.length > 0) {
                const examId = this.data.examEndlist[0].id; // 取第一个考试的 id
                this.getSchoolConfig(examId);
            }
            // 设置状态
            this.isLoading = false;
            this.isEnd = false;
        } catch (error) {
            // 处理请求失败
            console.error('请求失败:', error);
            wx.showToast({
                title: '请求失败',
                icon: 'error',
                duration: 2000
            });
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getexamContent()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },
    onRetry() {
    }
})